/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.patents.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.patents.entity.Patents;
import com.thinkgem.jeesite.modules.patents.service.PatentsService;

import java.sql.Array;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 查询专利信息Controller
 * @author wyy
 * @version 2018-10-24
 */
@Controller
@RequestMapping(value = "${adminPath}/patents/patents")
public class PatentsController extends BaseController {

	@Autowired
	private PatentsService patentsService;
	
	@ModelAttribute
	public Patents get(@RequestParam(required=false) String id) {
		Patents entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = patentsService.get(id);
		}
		if (entity == null){
			entity = new Patents();
		}
		return entity;
	}
	
	@RequiresPermissions("patents:patents:view")
	@RequestMapping(value = {"list", ""})
	public String list(Patents patents, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<Patents> list =patentsService.findList(patents);
		Page<Patents> page = patentsService.findPage(new Page<Patents>(request, response), patents);
		model.addAttribute("page", page);
		model.addAttribute("list", list);
		return "modules/patents/patentsList";

	}

	@RequiresPermissions("patents:patents:view")
	@RequestMapping(value = "form")
	public String form(Patents patents, Model model) {
		model.addAttribute("patents", patents);
		return "modules/patents/patentsForm";
	}

	@RequiresPermissions("patents:patents:edit")
	@RequestMapping(value = "save")
	public String save(Patents patents, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, patents)){
			return form(patents, model);
		}
		patentsService.save(patents);
		addMessage(redirectAttributes, "保存专利信息成功");
		return "redirect:"+Global.getAdminPath()+"/patents/patents/?repage";
	}
	
	@RequiresPermissions("patents:patents:edit")
	@RequestMapping(value = "delete")
	public String delete(Patents patents, RedirectAttributes redirectAttributes) {
		patentsService.delete(patents);
		addMessage(redirectAttributes, "删除查询专利信息成功");
		return "redirect:"+Global.getAdminPath()+"/patents/patents/?repage";
	}

	/**
	 * 年度趋势分析
	 **/
	@RequiresPermissions("patents:patents:view")
	@RequestMapping(value = "connector")
	public String connector(Patents patents, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		Map<String, Object> map1 = Maps.newHashMap();
		String dateStr = new DateTime().toString("yyyy");//系统时间
		int datetime = 0;
		for (int i=0;i<10;i++){
			int s = i+1;
			patents = new Patents();
			patents.setDatetime(dateStr);
			List<Patents> countUserYear = patentsService.getCountList(patents);
			patents.setUserType("大专院校");
			List<Patents> universitiesYearSum = patentsService.getCountList(patents);
			map1.put("year"+s,dateStr);
			map1.put("Count"+s,countUserYear.get(0).getCount());
			map1.put("Universities"+s,universitiesYearSum.get(0).getCount());
			System.out.println(map1);

			datetime = Integer.parseInt(dateStr);
			datetime -=1;
			dateStr = String.valueOf(datetime);
		}
		mapList.add(map1);
		model.addAttribute("mapList", mapList);
		return "modules/patents/connector";
	}
}